﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Twi.COMMON.Core
{
    /*
    * Copyright: ©2013 踏影软件开发工作室【Tyingsoft、Twilight】版权所有
    * Author: xuzhihong
    * Create date: 2010-10-28
    * Description: TwiAjaxHelper 所用到的Ajax类实体
    * 
    */
    public class AjaxModel
    {
        public AjaxModel()
        {
            dictParams = new Dictionary<string, object>();
        }

        /// <summary>
        /// 【模块名缩写，如SM】ModuleName:(mn)
        /// </summary>
        public string mn
        {
            get;
            set;
        }

        /// <summary>
        /// 【类名】ClassName(cn)
        /// </summary>
        public string cn
        {
            get;
            set;
        }

        /// <summary>
        /// 【方法名】MethodName(mn2)
        /// </summary>
        public string mn2
        {
            get;
            set;
        }

        /// <summary>
        /// 【是否为静态方法】isStatic
        /// </summary>
        public string isStatic
        {
            get;
            set;
        }

        /// <summary>
        /// 对应参数【参数名和方法中对应】
        /// </summary>
        public Dictionary<string, object> dictParams
        {
            get;
            set;
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddParam(string key, object value)
        {
            if (dictParams == null)
            {
                dictParams = new Dictionary<string, object>();
            }
            dictParams.Add(key, value);
        }

        public string TwiPageNS
        {
            get;
            set;
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public void WriteLog() 
        {
            if (TwiLogHelper.GetTwiLogMode() == TwiLogModel.Debugger)
            {
                string msg = "请求信息：";
                msg += string.Format("\r\n\t{0}:{1}","mn",mn);
                msg += string.Format("\r\n\t{0}:{1}", "cn", cn);
                msg += string.Format("\r\n\t{0}:{1}", "mn2", mn2);
                msg += "\r\n\r\n\t对应参数：";

                foreach(var kvp in dictParams)
                {
                    msg += string.Format("\r\n\t{0}:{1}", kvp.Key, "" + kvp.Value);
                }
                TwiLogHelper.Info(msg);
            } 
        }
    }


    /// <summary>
    /// 和AjaxModel 一样，专门给WPF使用
    /// </summary>
    public class RequestModel : AjaxModel
    {
        public XElement ToXElement(string rootNodeName = "RequestModel")
        {
            XElement xe = new XElement(rootNodeName);
            xe.SetElementValue("mn", this.mn);
            xe.SetElementValue("cn", this.cn);
            xe.SetElementValue("mn2", this.mn2);
            xe.SetElementValue("TwiPageNS", this.TwiPageNS);
            XElement xeParamList = new XElement("dictParams");
            foreach (KeyValuePair<string, object> kvp in dictParams)
            {
                if (kvp.Value.GetType().Name == "XElement")
                {
                    xeParamList.SetElementValue(kvp.Key, "");
                    xeParamList.Element(kvp.Key).Add(kvp.Value);
                }
                else
                {
                    xeParamList.SetElementValue(kvp.Key, kvp.Value);
                }
            }
            xe.Add(xeParamList);
            return xe;
        }
    }
}
